package com.lianzai.mobile.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lianzai.common.executor.LocalSvrSyncExecutor;
import com.lianzai.mobile.dao.CommunityInfoDao;
import com.lianzai.mobile.domain.CommunityInfoDO;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.lianzai.mobile.dao.CarPriceRuleDao;
import com.lianzai.mobile.domain.CarPriceRuleDO;
import com.lianzai.mobile.service.CarPriceRuleService;


@Service
public class CarPriceRuleServiceImpl implements CarPriceRuleService {
	@Autowired
	private CarPriceRuleDao carPriceRuleDao;

	@Autowired
	private CommunityInfoDao communityInfoDao;

	@Override
	public CarPriceRuleDO get(Long id){
		return carPriceRuleDao.get(id);
	}

	@Override
	public List<CarPriceRuleDO> list(Map<String, Object> map){
		return carPriceRuleDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return carPriceRuleDao.count(map);
	}

	@Override
	public int save(CarPriceRuleDO carPriceRule){
		return carPriceRuleDao.save(carPriceRule);
	}

	@Override
	public int update(CarPriceRuleDO carPriceRule){
		int ret = carPriceRuleDao.update(carPriceRule);
		if(1 == ret){//如果修改成功 则触发下发操作
			Map params = new HashedMap();
			params.put("ruleid",carPriceRule.getId());
			params.put("type",carPriceRule.getTypeInput());
			List<Map> commnityList = carPriceRuleDao.queryRuleCommunityList(params);
			for(Map map:commnityList){
				Long communityId = Long.parseLong(String.valueOf(map.get("id")));
				CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(communityId+""));
				JSONObject downJsonObject = new JSONObject();
				downJsonObject.put("communityId",communityId);
				LocalSvrSyncExecutor.bindRuleWithLocalDownload(downJsonObject,communityInfoDO);
			}
		}
		return ret;
	}

	@Override
	public int remove(Long id){
		return carPriceRuleDao.remove(id);
	}

	@Override
	public int batchRemove(Long[] ids){
		return carPriceRuleDao.batchRemove(ids);
	}

	@Override
	public int saveLs(Map map){
		return carPriceRuleDao.saveLs(map);
	}

	@Override
	public int updateLs(CarPriceRuleDO carPriceRule){
		int ret = carPriceRuleDao.updateLs(carPriceRule);
		if(1 == ret){//如果修改成功 则触发下发操作
			Map params = new HashedMap();
			params.put("ruleid",carPriceRule.getId());
			params.put("type",carPriceRule.getType());
			List<Map> commnityList = carPriceRuleDao.queryRuleCommunityList(params);
			for(Map map:commnityList){
				Long communityId = Long.parseLong(String.valueOf(map.get("id")));
				CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(communityId+""));
				JSONObject downJsonObject = new JSONObject();
				downJsonObject.put("communityId",communityId);
				LocalSvrSyncExecutor.bindRuleWithLocalDownload(downJsonObject,communityInfoDO);
			}
		}
		return ret;
	}

	@Override
	public int saveLsSegRule(Map map){
		return carPriceRuleDao.saveLsSegRule(map);
	}

	@Override
	public List<Map> getSegInfo(long ruleId){
		return carPriceRuleDao.getSegInfo(ruleId);
	}

	@Override
	public int removeSegInfo(long ruleId){
		return carPriceRuleDao.removeSegInfo(ruleId);
	}

	@Override
	public int bind(long id,long ruleId,int type){
		Map map = new HashedMap();
		map.put("id",id);
		map.put("ruleId",ruleId);
		map.put("type",type);
		int ret = carPriceRuleDao.bind(map);
		if(1==ret){
			//进行同步到本地操作
			CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(id+""));
			JSONObject downJsonObject = new JSONObject();
			downJsonObject.put("communityId",id);
			downJsonObject.put("ruleId",ruleId);
			downJsonObject.put("type",type);
			if(ruleId != -1){//不是解绑操作
				CarPriceRuleDO cpro = carPriceRuleDao.get(ruleId);//查询规则明细
				downJsonObject.put("ruleObject",cpro);
				if(type == 4){//如果是临时规则，则还要把临时规则的分段明细下发下去
					List<Map> segList = carPriceRuleDao.getSegInfo(ruleId);
					downJsonObject.put("ruleSegList",segList);
				}
			}
			LocalSvrSyncExecutor.bindRuleOper(downJsonObject,communityInfoDO);
		}
		return ret;
	}

	@Override
	public List<Map> cloudlist(String[] ids){
		return carPriceRuleDao.cloudlist(ids);
	}

	@Override
	public List<Map<String, Object>> queryRuleCommunityList(Map<String, Object> map){
		return carPriceRuleDao.queryRuleCommunityList(map);
	}

	@Override
	public int saveWithId(CarPriceRuleDO carPriceRule){
		return carPriceRuleDao.saveWithId(carPriceRule);
	}

	@Override
	public int removeAll(){
		return carPriceRuleDao.removeAll();
	}

	@Override
	public int removeAllSegInfo(){
		return carPriceRuleDao.removeAllSegInfo();
	}
}
